package com.ruoyi.sl.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.sl.domain.SlEvent;
import com.ruoyi.sl.service.ISlEventService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sl.domain.SlDbinfo;
import com.ruoyi.sl.service.ISlDbinfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import static com.ruoyi.common.utils.DateUtils.getNowDate;

/**
 * 待办Controller
 * 
 * @author admin
 * @date 2024-06-04
 */
@RestController
@RequestMapping("/sl/dbinfo")
public class SlDbinfoController extends BaseController
{
    @Autowired
    private ISlDbinfoService slDbinfoService;

    @Autowired
    private ISlEventService slEventService;

    /**
     * 查询待办列表
     */
    @PreAuthorize("@ss.hasPermi('sl:dbinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(SlDbinfo slDbinfo)
    {
        startPage();
        //只查询登录用户自己的
        slDbinfo.setTransactorsUser(getLoginUser().getUsername());
        List<SlDbinfo> list = slDbinfoService.selectSlDbinfoList(slDbinfo);
        return getDataTable(list);
    }

    /**
     * 导出待办列表
     */
    @PreAuthorize("@ss.hasPermi('sl:dbinfo:export')")
    @Log(title = "待办", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SlDbinfo slDbinfo)
    {
        List<SlDbinfo> list = slDbinfoService.selectSlDbinfoList(slDbinfo);
        ExcelUtil<SlDbinfo> util = new ExcelUtil<SlDbinfo>(SlDbinfo.class);
        util.exportExcel(response, list, "待办数据");
    }

    /**
     * 获取待办详细信息
     */
    @PreAuthorize("@ss.hasPermi('sl:dbinfo:query')")
    @GetMapping(value = "/{dbId}")
    public AjaxResult getInfo(@PathVariable("dbId") Long dbId)
    {

        AjaxResult ajax = AjaxResult.success();
        SlDbinfo slDbinfo = slDbinfoService.selectSlDbinfoByDbId(dbId);
        //如果是汇报hb,获取汇报详情
        if(slDbinfo.getSource().equals("hb")){
            ajax.put("hbInfo", slEventService.selectSlEventByEventId(slDbinfo.getSourceId()));
        }
        ajax.put(AjaxResult.DATA_TAG, slDbinfo);
        return ajax;
    }

    /**
     * 新增待办
     */
    @PreAuthorize("@ss.hasPermi('sl:dbinfo:add')")
    @Log(title = "待办", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SlDbinfo slDbinfo)
    {
        slDbinfo.setCreateBy(getUsername());
        return toAjax(slDbinfoService.insertSlDbinfo(slDbinfo));
    }

    /**
     * 修改待办
     */
    @PreAuthorize("@ss.hasPermi('sl:dbinfo:edit')")
    @Log(title = "待办", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SlDbinfo slDbinfo)
    {
        slDbinfo.setUpdateBy(getUsername());
        return toAjax(slDbinfoService.updateSlDbinfo(slDbinfo));
    }

    /**
     * 报工单审核通过
     */
    @PreAuthorize("@ss.hasPermi('sl:dbinfo:edit')")
    @Log(title = "待办", businessType = BusinessType.UPDATE)
    @GetMapping("/bg/{dbId}")
    public AjaxResult approvedDbinfo(@PathVariable Long dbId)
    {
        return toAjax(slDbinfoService.approvedDbinfo(dbId));
    }

    /**
     * 报工单复核通过
     */
    @PreAuthorize("@ss.hasPermi('sl:dbinfo:edit')")
    @Log(title = "待办", businessType = BusinessType.UPDATE)
    @GetMapping("/check/{dbId}")
    public AjaxResult checkPassDbinfo(@PathVariable Long dbId)
    {
        return toAjax(slDbinfoService.checkPassDbinfo(dbId));
    }


    /**
     * 删除待办
     */
    @PreAuthorize("@ss.hasPermi('sl:dbinfo:remove')")
    @Log(title = "待办", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dbIds}")
    public AjaxResult remove(@PathVariable Long[] dbIds)
    {
        return toAjax(slDbinfoService.deleteSlDbinfoByDbIds(dbIds));
    }

    /**
     * 完结待办
     */
    @PreAuthorize("@ss.hasPermi('sl:dbinfo:edit')")
    @GetMapping(value = "/done/{dbId}")
    public AjaxResult doneInfo(@PathVariable("dbId") Long dbId)
    {

        SlDbinfo slDbinfo = slDbinfoService.selectSlDbinfoByDbId(dbId);
        slDbinfo.setIsDone("2");
        slDbinfo.setIsDb("1");
        slDbinfo.setDoneTime(getNowDate());
        //int row = slDbinfoService.updateSlDbinfo(slDbinfo);

        //修改汇报单状态
        SlEvent slEvent= slEventService.selectSlEventByEventId(slDbinfo.getSourceId());
        slEvent.setIsDone("2");
        slEvent.setDoneTime(getNowDate());
        slEventService.updateSlEvent(slEvent);


        return toAjax(slDbinfoService.updateSlDbinfo(slDbinfo));

    }


}
